package com.shop.Utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtils {
    /*设置过期时间*/
    private static final long EXPIRE_DATE=30*60*100000;
    //密钥
//    private static final String TOKEN_SECRET = "ruanJian2001Shop";
    private static final String TOKEN_SECRET = RandomStringUtils.randomAlphanumeric(15);
    public static String token (String username,String password){

        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");
            //携带username，password信息，生成签名
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("username",username)
                    .withClaim("password",password).withExpiresAt(date)
                    .sign(algorithm);
        }catch (Exception e){
            return  null;
        }
        return token;
    }
    public static boolean verify(String token){
        /**
         * @desc   验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        }catch (Exception e){
            return  false;
        }
    }
    public static String[] yanzhen(String token){
       if (verify(token)){
          return JieMa(token);
       }

        return null;
    }
    public static String[] JieMa(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            String[] strArr = new String[2];
            strArr[0] = jwt.getClaim("username").asString();
            strArr[1] = jwt.getClaim("password").asString();
            return strArr;
        }catch (Exception e){
            return null;
        }

    }


    public static void main(String[] args) {
        String username ="zhangsan";
        String password = "123";
        String token = token(username,password);
        System.out.println(token);
        boolean b = verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6ImI5Y2Y3ZTFhMmU3NTI1OTk3Mjg1OWU1ZWFlZTQ3NGQ2IiwiZXhwIjoxNjM4MTA2NDU2LCJ1c2VybmFtZSI6ImFkbWluMSJ9.uFUDkKvDvc7jDUIH2SPMYUCBS3mowwdJ1xtqWwqHCdw");

        System.out.println(Arrays.toString(yanzhen(token)));
    }

}
